using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Modules.NBrightEspace.Components;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using NBrightCore.common;
using NBrightCore.render;
using NBrightDNN;

namespace DotNetNuke.Modules.NBrightEspace
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewNBrightEspace class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : EspaceModuleBase
    {

        #region Event Handlers

        private const string EntityTypeCode = "CLIENT";
        private const string CtrlTypeCode = "CLIENTVIEW";

        override protected void OnInit(EventArgs e)
        {

            //Set base EntityTypeCode, MUST be done for all NBright child ctrls before OnInit
            base.EntityTypeCode = EntityTypeCode;
            base.CtrlTypeCode = CtrlTypeCode;

            base.OnInit(e);

            // turn off menu
            base.HideMenu();

            //initalize the repeater template based on what we want to see, a list or editing details.
            if (UserInfo.IsInRole("Manager"))
            {// itemid is blank so display the list
                rpData.ItemTemplate = new GenXmlTemplate(TemplCtrl.GetTemplateData("BackOfficeEntry.html", Utils.GetCurrentCulture()));
            }
            else
            {// itemid is not blank, so display the detail template.
                rpData.ItemTemplate = new GenXmlTemplate(TemplCtrl.GetTemplateData("CLIENTVIEW_Detail.html", Utils.GetCurrentCulture()));
            }
        }


        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);
            if (Page.IsPostBack == false)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            try
            {
                if (UserInfo.IsInRole("Manager"))
                {
                    //display backoffice entry 
                    //var obj = new NBrightInfo();
                    //var objL = new List<NBrightInfo> { obj };
                    //rpData.DataSource = objL;
                    //rpData.DataBind();
                    base.DoDetail(rpData);
                }
                else
                {
                    //Display Client View
                    var strFilter = " and ([xmlData].value('(genxml/textbox/txtusername)[1]', 'nvarchar(20)') = '" + UserInfo.Username + "')";
                    var objL = ObjCtrl.GetList(-1,-1,"CLIENT", strFilter);
                    if (objL.Count == 1)
                    {
                        var obj = objL[0];
                        base.ItemId = obj.ItemID.ToString("");
                        base.DoDetail(rpData);
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region  "Events "

        protected void CtrlItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var cArg = e.CommandArgument.ToString();

            switch (e.CommandName.ToLower())
            {
                case "enter":
                    if (UserInfo.IsInRole("Manager"))
                    {
                        Response.Redirect(EditUrl("dashboard"));                        
                    }
                    break;
                case "home":
                    Response.Redirect(Context.Request.Url.Scheme + "://" + Context.Request.Url.Host);
                    break;
            }

        }

        #endregion

    }

}
